<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :titles="['流行', '新款', '精选']" ref="tabcontrol1" @tabclick="tabclick" class="tab-control"
      v-show="isTabFixed" />
    <scroll class="content" ref="scroll" :probe-type="3" :pull-up-load="true" @scroll="contentScroll"
      @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>
      <home-recom-view :recommends="recommends"></home-recom-view>
      <home-feature-view></home-feature-view>
      <tab-control :titles="['流行', '新款', '精选']" ref="tabcontrol2" class="tab-control" @tabclick="tabclick"></tab-control>
      <goods-list :goods="showGoods"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
// 共用的
import NavBar from '@/components/common/navbar/NavBar.vue'
import { getHomeMultidata, getHomeGoods } from '@/network/home.js'
import TabControl from '@/components/content/tabControl/TabControl.vue'
import GoodsList from '@/components/content/goods/GoodsList.vue'
import Scroll from '@/components/common/scroll/Scroll.vue'
// 子组件
import HomeSwiper from '@/views/home/childComps/HomeSwiper.vue'
import HomeRecomView from '@/views/home/childComps/HomeRecomView.vue'
import HomeFeatureView from '@/views/home/childComps/HomeFeatureView.vue'

// 工具操作
import { itemListenerMixin, backTopMixin } from '@/utils/mixin'

export default {
  name: 'Home',
  data() {
    return {
      banners: [],
      recommends: [],
      // 直接全部请求过来
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] }
      },
      currentType: 'pop',
      tabOffsetTop: 0,
      isShowBackTop: false,
      isTabFixed: false,
      saveY: 0,
    }
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0)
    // 重新计算页面高度 防止页面假死
    this.$refs.scroll.refresh()
  },
  deactivated() {
    // 1. 保存Y的值
    // 保存值 防止页面假死
    this.saveY = this.$refs.scroll.scrollY
    // 2.取消全局事件的监听
    this.$bus.$off('itemImageLoad', this.itemImgListener)

  },
  components: {
    // 注册组件
    NavBar,
    HomeSwiper,
    HomeRecomView,
    HomeFeatureView,
    TabControl,
    GoodsList,
    Scroll,
  },
  mixins: [itemListenerMixin, backTopMixin],
  mounted() {
    // 在组件中不可以使用create 调用频繁
    // 利用debounce来执行操作 去防抖的操作
    /*  const refresh = debounce(this.$refs.scroll.refresh, 500)
     
     // 保存监听事件
     this.itemImgListener = () =>{
       refresh()
     }
     
     this.$bus.$on('itemImageLoad',this.itemImgListener) */
  },
  created() {
    // 请求多个数据
    this.getHomeMultidata()
    // 请求不同商品的数据
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')
  },
  methods: {
    getHomeMultidata() {
      getHomeMultidata()
        .then(response => {
          // 格式化数据
          this.banners = response.data.banner.list
          this.recommends = response.data.recommend.list
        })
        .catch(err => {
          console.log(err)
        })
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1
      getHomeGoods(type, page).then(res => {
        // 格式化数据
        this.goods[type].list.push(...res.data.list)
        this.goods[type].page += 1
        // 上拉加载刷新 需要重新计算高度 防止页面假死
        this.$refs.scroll.finishPullUp()
      })
        .catch(err => {
          console.log(err)
        })
    },
    tabclick(index) {
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      // 点击数据的index进行点击换取
      // 两个tabcontril保持一直的index这样会随时进行变换
      this.$refs.tabcontrol1.currentIndex = index
      this.$refs.tabcontrol2.currentIndex = index
    },
    contentScroll(position) {
      // 1. 判断BackTop显示隐藏
      // this.isShowBackTop = -position.y > 1000 ? true : false
      this.ListenerShowBackTop(position)
      // 2. 判断是否吸顶 
      this.isTabFixed = -(position.y) >= this.tabOffsetTop ? true : false
    },
    loadMore() {
      this.getHomeGoods(this.currentType)
      // 刷新重新计算高度 防止页面假死
      this.$refs.scroll.scroll.refresh()
    },

    swiperImageLoad() {
      // 3. 获取
      // 这里重新获取数据 
      // 父子组件的通信
      // 所有的组件都有一个属性 $el :用于获取组件中的元素
      this.tabOffsetTop = this.$refs.tabcontrol2.$el.offsetTop
    }
  },
  computed: {
    // 计算属性 获取购物数据的操作
    // 将商品数据进行格式化操作 数据进行改变的时候进行页面的刷新
    showGoods() {
      return this.goods[this.currentType].list
    }
  }
}
</script>

<style scoped>
#home {
  padding: 0;
  margin: 0;
  height: 100vh;
  position: relative;
}

.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  z-index: 1;
}

.content {
  position: absolute;
  overflow: hidden;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}

.tab-control {
  margin: 0;
  padding: 0;
  position: relative;
  z-index: 9;
}

.content {
  height: calc(100% - 44px - 49px);
  overflow: hidden;
}
</style>